package com.mt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mt.VO.ExpectNumVO;
import com.mt.constants.Result;
import com.mt.pojo.Film;
import com.mt.pojo.FilmTag;
import com.mt.service.ExpectNumService;
import com.mt.service.FilmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Wang
 * @since 2020-11-12
 */
@CrossOrigin
@RestController
@RequestMapping("/film")
@Slf4j
public class FilmController {
    @Autowired
    FilmService filmService;

    @Autowired
    ExpectNumService expectNumService;


    @GetMapping("/tags")
    public Result getTags() {
        List<FilmTag> tags = filmService.getTags();
        Result<Object> result = new Result<>();
        result.success(tags);
        return result;
    }

    //获取所有
    @GetMapping
    public Result listFilms(int currentPage, int pageSize) {
        log.debug("传入参数: currentPage:{} , pageSize:{}",currentPage,pageSize);
        List<Film> films = filmService.listFilms(currentPage, pageSize);
        Result<List<Film>> result = new Result<>();
        result.success(films);
        return result;
    }

    @GetMapping("/all")
    public Result getAllFilms() {
        List<Film> films = filmService.getAllFilms();
        Result<List<Film>> result = new Result<>();
        result.success(films);
        return result;
    }

    //管理端需求,分页控制
    @GetMapping("/page")
    public Result pageFilms(int currentPage, int pageSize,String filmName) {
        log.debug("传入参数: currentPage:{} , pageSize:{}",currentPage,pageSize);
        IPage<Film> films = filmService.pageFilms(currentPage,pageSize,filmName);
        Result<Object> result = new Result<>();
        result.success(films);
        return result;
    }

    //用户端需求:获取指定ID的电影信息(有排片上映的)
    @GetMapping("list")
    public Result getFilmsById(@RequestParam("ids")List<Integer> ids) {
        log.debug("传入参数: ids:{} ",ids);
        List<Film> films = filmService.getFilmsById(ids);
        Result<List<Film>> result = new Result<>();
        result.success(films);
        return result;
    }

    // 最近上映的电影
    @GetMapping("/recentlyRelease")
    public Result listRecentlyReleaseFilms() {
        Result<List<Film>> result = new Result<>();
        List<Film> films = filmService.listRecentlyReleaseFilms();
        result.success(films);
        return result;
    }

    //筛选
    public Result searchFilm(int currentPage, int pageSize, List<Integer> tagIds, String releaseYear) {
        log.debug("传入参数: currentPage:{} , pageSize:{},tagIds:{} , releaseYear:{}",currentPage,pageSize,tagIds,releaseYear);
        List<Film> films = filmService.searchFilm(currentPage, pageSize,tagIds,releaseYear);
        Result<List<Film>> result = new Result<>();
        result.success(films);
        return result;
    }

    @GetMapping("/search")
    public Result searchFilm(String filmName) {
        log.debug("模糊搜索KEY: "+filmName);
        List<Film> films = filmService.searchFilm(filmName);
        Result<List<Film>> result = new Result<>();
        result.success(films);
        return result;
    }

    @GetMapping("detail")
    public Result getFilmDetail(@RequestParam("filmId") Integer filmId,@RequestParam("userId")String userId) {
        log.debug("传入参数:{}",filmId);
        Result<Object> result = new Result<>();
        Film detail = filmService.getFilmDetail(filmId,userId);
        result.success(detail);
        return result;
    }
    @GetMapping("/viewer/detail")
    public Result getFilmDetail(@RequestParam("filmId") Integer filmId) {
        log.debug("传入参数:{}",filmId);
        Result<Object> result = new Result<>();
        Film detail = filmService.getFilmDetail(filmId,"");
        result.success(detail);
        return result;
    }


    @PostMapping("/expect")
    public Result addExpectFilm(@RequestBody ExpectNumVO expectNum, @RequestParam("userId")String userId){
        expectNum.setUserId(userId);
        Result<Object> result = new Result<>();
        boolean addExpect2Redis = expectNumService.addExpect2Redis(expectNum.getUserId(),
                expectNum.getFilmId(),
                expectNum.getStatus());
        result.success(addExpect2Redis);
        return result;
    }

    @PostMapping("/add")
    public Result addFilm(@RequestBody Film film) {
        log.debug("传入参数:{}",film.toString());
        Result<Object> result = new Result<>();
        if (filmService.addFilm(film)) {
            result.success();
        }
        return result;
    }
    @PostMapping("/modify")
    public Result modifyFilm(@RequestBody Film film) {
        log.debug("传入参数:{}",film.toString());
        Result<Object> result = new Result<>();
        if (filmService.modifyFilm(film)) {
            result.success();
        }
        return result;
    }

    @GetMapping("/topfilms")
    public Result getTopFilm() {
        Result<Object> result = new Result<>();
        List<Film> topFilms = filmService.getTopFilm();
        result.success(topFilms);
        return result;
    }

}
